﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net.NetworkInformation;
using System.IO;
using System.Text;
using System;


public class authorization
{
    private static string singkey = "AUTHOR_0ooo0o0o000o0ooo00o0o0o0o0o0ooo0o0oo0o000o0o_ZATION";
    public string errorString = "";
    public string key = "";

    //源文
    private static string authorizpath = "authorization.a";
    //密块文
    //private static string authorizMDpath = "authorization.b";

    public authorization(string k = null)
    {
        key = k;

        if (key == null)
            key = System.Guid.NewGuid().ToString("N");

     
        //初始化
        string au = readfiles(Application.persistentDataPath, authorizpath);
         
        if (au == null || au=="")
            writefiles(Application.persistentDataPath, authorizpath, key);
        else
         key = au;
        
    }

    //
    public string markCode(){
             return  readfiles(Application.persistentDataPath, "mark.a");
    }

    public void SetmarkCode(string code){
         writefiles(Application.persistentDataPath, "mark.a", code);
    }

    //是否有权限锁
    public bool isLock()
    {
        string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "loook");
        return System.IO.File.Exists(filePath);

    }

    //加是权限锁
    public bool toLock()
    {
       string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "loook");
        using (System.IO.FileStream stream = System.IO.File.Open(filePath, System.IO.FileMode.OpenOrCreate))
        {
            byte[] str = System.Text.Encoding.Default.GetBytes("info");
            stream.Write(str, 0, str.Length);
            stream.Flush();
            stream.Close();
        }
        return true;
    }

    public static string getMark(string datas){
        //  Debug.Log("get mark:"+ datas);
         return AuSecurity.Get_Md_32(datas);
    }
    //设备码
    public  string getDivecCode()
    {
        string a = "";
        a += DiskID.GetDiskVolume("C");
        a += singkey;
        a += key;
        return AuSecurity.Get_Md_32(a);
    }

    //检测授权
    public bool checkAuthorization()
    {

        return false;
    }

    //y读取文件
    public static string readfiles(string path, string name)
    {
        string tx = "";
        //文件路径
        string filePath = System.IO.Path.Combine(path, name);
         System.IO.Directory.CreateDirectory(path);
        if (System.IO.File.Exists(filePath))
        {
           // Debug.LogError("readfiles "+filePath);
            StreamReader sr = new StreamReader(filePath, Encoding.Default);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                tx += line.ToString();
            }
            sr.Close();
           // Debug.LogError("readfiles datas "+tx);
            return tx;

        }
        return tx;
    }
    // 写入文件
    public static void writefiles(string path, string name, string info)
    {
        string filePath = System.IO.Path.Combine(path, name);
        System.IO.Directory.CreateDirectory(path);
        Debug.LogError("write:"+filePath);
        using (System.IO.FileStream stream = System.IO.File.Open(filePath, System.IO.FileMode.OpenOrCreate))
        {
            byte[] str = System.Text.Encoding.Default.GetBytes(info);
            stream.Write(str, 0, str.Length);
            stream.Flush();
            stream.Close();
        }
    }

    //读取当前的key 值
    public string getAutorizationKey()
    {
        return key;
    }
    
    //验证时间
    public bool isValidTime(AuConfig au){
             DateTime dt =DateTime.Now;
             //格式 "2018-3-4"
             DateTime ds =Convert.ToDateTime(au.authorizTime); 
             //时间小于授权时间
            if(DateTime.Compare(dt,ds)<0)
                return false;

            DateTime de=Convert.ToDateTime(au.authorizValidTime);
             //超出有效时间
             if(DateTime.Compare(dt,de)>0)
                return false;

         return true;
    }

    //信息内容验证
    public string SecurityCode(AuConfig cn){
            return  AuSecurity.Get_Md_32(cn.GetInfo()+markCode()+getDivecCode()+getAutorizationKey()+singkey);   
    }

    //检测权限值 返回false 不通过 查看 errorString
    //1 检测验证key正确性
    //2 检测key 授权类型 
    //3 检测key 授权时间是否有超时 
    public bool isCheck( AuConfig ac)
    { 
            string code=   SecurityCode(ac); 
        return (ac.authorizMaskCode==code);
    }

    public string device()
    {
        string mar = "";

        string a = SystemInfo.deviceUniqueIdentifier;
        string a1 = SystemInfo.deviceName;
        string b = SystemInfo.graphicsDeviceID.ToString();
        string b1 = SystemInfo.graphicsDeviceVendorID.ToString();

        mar = AuSecurity.Get_Md_32(a + a1 + b + b1);
        return mar;
    }

    public string mac()
    {
        NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface ni in nis)
        {
            Debug.Log("Name = " + ni.Name);
            Debug.Log("Des = " + ni.Description);
            Debug.Log("Type = " + ni.NetworkInterfaceType.ToString());
            Debug.Log("Mac = " + ni.GetPhysicalAddress().ToString());
            Debug.Log("------------------------------------");
        }
        return null;
    }


    public static string ConfigFile(){
         return System.IO.Path.Combine(Application.streamingAssetsPath, "auconfig.json");
    }
     // 加载配置文件
    public static  AuConfig LoadConfigDatas (string path=null) {
         if(path==null)
           path=ConfigFile();

        if (!File.Exists (path))
            return new AuConfig();

        StreamReader sr = new StreamReader (path);
        if (sr == null)
            return new AuConfig();

        string json = sr.ReadToEnd ();
          sr.Close();

        if (json.Length > 0)
            return JsonUtility.FromJson<AuConfig> (json);
        return new AuConfig();
    }

    //写入配置文件
   public static  void writeConfigDatas (AuConfig data,string path=null) {
        if(path==null)
           path=ConfigFile();
        string json = JsonUtility.ToJson (data, true);
        File.WriteAllText (path, json, Encoding.UTF8);
    
    }
}
